package com.top.mall.main.mine

import android.widget.CheckBox
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.entity.Address
import com.top.mall.entity.Leader
import com.top.mall.jetpack.vm.MineViewModel
import kotlinx.android.synthetic.main.activity_leader.*

class MineLeaderActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MineLeaderActivity::class.java)
        }
    }

    private val mLeaderAdapter =
        object : BaseQuickAdapter<Leader, BaseViewHolder>(R.layout.item_leader) {
            override fun convert(holder: BaseViewHolder, item: Leader) {
                holder.setText(R.id.tvLeaderName, "姓名：${item.surname}${item.user_name}")
                    .setText(R.id.tvLeaderPhone, item.mobile)
                    .setText(R.id.tvLeaderCard, "身份证/护照号：${item.id_card}")
                    .setText(R.id.tvLeaderTime, "出生日期：${item.birthday}")
                    .getView<CheckBox>(R.id.cbLeaderSelect)
                    .isChecked = item.is_default == "1"
            }
        }

    override val layoutResId: Int
        get() = R.layout.activity_leader

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.colorWhite, view = flLeaderBar)
    }

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }

        tvLeaderAdd.setOnClickListener {
            LeaderSaveActivity.open()
        }

        rvLeaderBody.adapter = mLeaderAdapter
    }

    override fun onResume() {
        super.onResume()
        viewModel.leaderList()
    }

    override fun initViewMode() {
        viewModel.leaderListLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mLeaderAdapter.setNewInstance(data)
        })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }
}